package com.thprojects.myphotomemories.view;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thprojects.myphotomemories.R;
import com.thprojects.myphotomemories.controller.DbController;
import com.thprojects.myphotomemories.controller.GuiController;
import com.thprojects.myphotomemories.model.Album;
import com.thprojects.myphotomemories.model.LoggedUser;
import com.thprojects.myphotomemories.view.components.AlbumPreviewList;

public class MyPhotoMemoriesActivity extends Activity {
	
	DbController mDbController;
	ProgressDialog mPDialog;
	List<Album> mAlbums;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //load albums
        LoadAlbumsTask at = new LoadAlbumsTask();
        at.execute(this);
    }
    
    
    /* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		
		MenuItem mi1 = menu.add("New Album");
		mi1.setIcon(R.drawable.menu_new_album);
		mi1.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				GuiController.showCreateAlbum(MyPhotoMemoriesActivity.this);
				return false;
			}
		});
		
		MenuItem mi2 = menu.add("Web Albums");
		mi2.setIcon(R.drawable.menu_web_albums);
		mi2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			public boolean onMenuItemClick(MenuItem item) {
				LoggedUser user = LoggedUser.getLoggedUser();
				if (user==null){
					GuiController.showLogin(MyPhotoMemoriesActivity.this);
				}
				else{
					GuiController.showWebPhotos(MyPhotoMemoriesActivity.this);
				}
				return false;
			}
		});
		
		return true;
	}
    
    private void showDialog(){
    	mPDialog = ProgressDialog.show(this, "", "Loading your albums...", true);
    }

    private void showAlbumList(){
    	LinearLayout cnt = (LinearLayout) findViewById(R.id.content);
    	
    	if (mAlbums.size() == 0){
    		TextView tv = new TextView(this);
    		tv.setText(R.string.no_album);
    		cnt.addView(tv);
    	}
    	else{
    		cnt.addView(new AlbumPreviewList(mAlbums, this));
    	}
    	
    }
    
    private class LoadAlbumsTask extends AsyncTask<Context, Void, List<Album>>{

		@Override
		protected List<Album> doInBackground(Context... params) {
			mDbController = DbController.getDbController(params[0]);
			return mDbController.getAlbumList();
		}
	
		@Override
		protected void onPostExecute(List<Album> result) {
			super.onPostExecute(result);
			mAlbums = result;
			mPDialog.dismiss();
			showAlbumList();
		}
	
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showDialog();
		}
    	
    }
}